﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;
using System.Security.Cryptography.X509Certificates;
using System.IO;

namespace DataAccess
{
    public partial class DataAccess
    {
        public DataAccessResult Swap(String SubscriptionID, String CertificatePath, String ServiceName,String ProductionName,String SourceDeploymentName )
        {
            DataAccessResult result = new DataAccessResult();

            string requestBodyFormat = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Swap xmlns=""http://schemas.microsoft.com/windowsazure"">
  <Production>{0}</Production>
  <SourceDeployment>{1}</SourceDeployment>
</Swap>";

            string requestUrl = "https://management.core.windows.net/" + SubscriptionID + "/services/hostedservices/"+ServiceName;
            String requestBody = string.Format(requestBodyFormat, ProductionName, SourceDeploymentName);
            string ReturnBody = string.Empty;
            WebResponse resp = null;
            try
            {
                NameValueCollection RequestHeaders = new NameValueCollection();
                RequestHeaders.Add("x-ms-version", x_ms_version);
                X509Certificate2 cert = new X509Certificate2();
                cert.Import(CertificatePath);
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
                request.Method = "POST";
                request.ClientCertificates.Add(cert);
                request.ContentType = "application/xml";
                request.ContentLength = Encoding.UTF8.GetBytes(requestBody).Length;
                if (RequestHeaders != null)
                {
                    request.Headers.Add(RequestHeaders);
                }

                using (StreamWriter sw = new StreamWriter(request.GetRequestStream()))
                {
                    sw.Write(requestBody);
                    sw.Close();
                }

                resp = request.GetResponse();
               
                if (resp != null)
                {

                    using (StreamReader sr = new StreamReader(resp.GetResponseStream(), true))
                    {
                        ReturnBody = sr.ReadToEnd();
                        sr.Close();
                    }
                    if (ReturnBody != null)
                    {

                        HttpWebResponse httpResp = (HttpWebResponse)resp;
                        result.IsOperationSuccessful = httpResp.StatusCode.Equals(HttpStatusCode.Accepted);
                    }
                }
            }
            catch (WebException webEx)
            {
                result.Error = webEx;
                result.IsOperationSuccessful = false;
            }
            catch (Exception excep)
            {
                result.Error = excep;
                result.IsOperationSuccessful = false;
            }
            finally
            {
                if (resp != null) { resp.Close(); }
                resp = null;
            }
            return result;
        }
    }
}
